package com.example.docking.httpJX;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionExample {
    private static final Logger logger = LoggerFactory.getLogger(HttpUrlConnectionExample.class);

    public String sendPostRequest(String urlStr, String jsonPayload) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);

        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = jsonPayload.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                return response.toString();
            }
        } else {
            throw new IOException("HTTP request failed with response code: " + responseCode);
        }
    }


    public String sendRequestApplyProduceOrder(String jsonPayload) {
        String url = "http://10.0.0.236:8000/api/JxWxtxApi/ApplyProduceOrder";
        // 这里使用之前生成的 PurRequestDao 对应的 JSON 作为示例请求体
        String response = "";
        try {
            logger.info("生产任务单：推送敬信url：: " + url);
            logger.info("生产任务单：推送敬信生产任务单数据：: " + jsonPayload);
            response = sendPostRequest(url, jsonPayload);
            logger.info("生产任务单：推送敬信返回：Response: " + response);
        } catch (IOException e) {

            e.printStackTrace();
        }
        return response;
    }

    public String sendRequestApplyOrderWgqd(String jsonPayload) {
        String url = "http://10.0.0.236:8000/api/JxWxtxApi/ApplyOrderWgqd";
        // 这里使用之前生成的 PurRequestDao 对应的 JSON 作为示例请求体
        String response = "";
        try {
            logger.info("外购清单：生产任务单：推送敬信url：: " + url);
            logger.info("外购清单：推送敬信生产任务单数据：: " + jsonPayload);
            response = sendPostRequest(url, jsonPayload);
            logger.info("外购清单：推送敬信返回：Response: " + response);
        } catch (IOException e) {

            e.printStackTrace();
        }
        return response;
    }

    public String sendRequestApplyOrderWcqd(String jsonPayload) {
        String url = "http://10.0.0.236:8000/api/JxWxtxApi/ApplyOrderWcqd";
        // 这里使用之前生成的 PurRequestDao 对应的 JSON 作为示例请求体
        String response = "";
        try {
            logger.info("外采清单：生产任务单：推送敬信url：: " + url);
            logger.info("外采清单：推送敬信生产任务单数据：: " + jsonPayload);
            response = sendPostRequest(url, jsonPayload);
            logger.info("外采清单：推送敬信返回：Response: " + response);
        } catch (IOException e) {

            e.printStackTrace();
        }
        return response;
    }

    public String sendRequestApplyOrderWxqd(String jsonPayload) {
        String url = "http://10.0.0.236:8000/api/JxWxtxApi/ApplyOrderWxqd";
        // 这里使用之前生成的 PurRequestDao 对应的 JSON 作为示例请求体
        String response = "";
        try {
            logger.info("外协清单：推送敬信url：: " + url);
            logger.info("外协清单：推送敬信生产任务单数据：: " + jsonPayload);
            response = sendPostRequest(url, jsonPayload);
            logger.info("外协清单：推送敬信返回：Response: " + response);
        } catch (IOException e) {

            e.printStackTrace();
        }
        return response;
    }

    public String sendRequestApplyStauts(String jsonPayload) {
        String url = "http://10.0.0.236:8000/api/JxWxtxApi/ApplyProduceOrder";
        // 这里使用之前生成的 PurRequestDao 对应的 JSON 作为示例请求体
        String response = "";
        try {
            response = sendPostRequest(url, jsonPayload);
            logger.info("推送敬信返回：Response: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    public static void main(String[] args) {
        HttpUrlConnectionExample httpUrlConnectionExample = new HttpUrlConnectionExample();
        String jsonPayload = "{\"bill_date\":\"2024-09-15\",\"emp\":\"EMP001\",\"emp_id\":\"EID001\",\"materials\":[{\"materialName\":\"Product X\",\"materialid\":\"PROD002\",\"unit\":\"boxes\",\"unitid\":\"UNIT003\",\"materialType\":\"Type B\",\"materialNumber\":200,\"plan_commit_date\":\"2024-09-20\",\"plan_finish_date\":\"2024-09-30\",\"subMaterial\":[{\"den_qty_scrap\":\"200\",\"num_qty_scrap\":\"30\",\"sub_qty\":80,\"sub_material_id\":\"MAT002\",\"sub_materialName\":\"Material 2\",\"sub_unit\":\"kg\",\"sub_unit_id\":\"UNIT004\"}],\"delivery_date\":\"2024-10-05\",\"apply_qty\":80}]}";

        httpUrlConnectionExample.sendRequestApplyProduceOrder(jsonPayload);
    }
}